<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button id="btn">提交</button>
    <script>
        let btn = document.getElementById('btn')
        function handle(e) {
            console.log('向后端发送请求');
            //将按钮设置为不可点击状态
            // btn.disabled = true

        }
        btn.addEventListener('click', debounce(handle, 1000))//监视请求函数，让其1秒后发送请求
        //防抖函数
        function debounce(fn, wait) {
            var timer = null//闭包保留住上一个定时器
            let that = this
            return function (...arg) {//...arg写在函数体形参上为特例，作用是将所有参数打包成arg的数组
                clearTimeout(timer)//销毁上一个定时器
                timer = setTimeout(function () {
                    fn.call(that, ...arg)
                    //call函数将fn内的this强行绑定为function的this,最终指向button
                    //解构arg将参数拿出来
                }, wait)
            }
        }
    </script>
</body>

</html>